#--------------------------------------------------------------------------------------------------------------------#
#----------------------------------------------- Portmann & Stojanovic ----------------------------------------------#
#--------------------------------------------------- November 2020 --------------------------------------------------#
#------- Are Immigrant-Origin Candidates Penalized in Virtue of Ingroup Favoritism or Outrgoup Hostility? -----------#
#-------------------------------------------- Appendix B: Data and Sample -------------------------------------------#
#--------------------------------------------------------------------------------------------------------------------#

# ------------------------------------------------------------------ Get data

rm(list=ls())
setwd(".../...")
load("d_nr15_gde")
load("d_nr15_cand")
load("d_initiatives_wd")

# ------------------------------------------------------------------ Table 2 Appendix: Overview municipalities in our sample by canton

# N municipalities

d_nr15_gde %>% 
ungroup() %>% 
dplyr::summarise(n = n_distinct(bfsresidence_code))

#Table A1 (number of municipalities in cantons)

d_raumglied <- read.csv("//unetna01/Portmale$/Daten/SwitchDrive/Dissertation/15_ED NR 2015/Revisions CPS/Replication files/BfsRaumglied.csv", sep=";")
d_raumglied <- d_raumglied[, c(1:3, 9, 16, 17, 19, 21, 22, 23, 25)]

AT1a <- d_raumglied %>% 
dplyr::select(bfscantno, bfsresidence_code) %>% 
dplyr::group_by(bfscantno) %>%
dplyr::summarise(N = n_distinct(bfsresidence_code))

AT1b <- d_nr15_gde %>% 
dplyr::select(canton.x, bfsresidence_code, bfscantno.y) %>% 
dplyr::group_by(canton.x, bfscantno.y) %>%
dplyr::summarise(n = n_distinct(bfsresidence_code))

AT1c <- merge(x = AT1a, y = AT1b, by.x = "bfscantno", by.y = "bfscantno.y", all.x = TRUE)
AT1c$n <- ifelse(is.na(AT1c$n), 0, AT1c$n)

names(AT1c)
AT1c <- AT1c %>% mutate(pct = (n/N)*100)
AT1c$canton <- ifelse(!is.na(AT1c$canton), AT1c$canton, ifelse(AT1c$bfscantno==4, "UR", 
ifelse(AT1c$bfscantno==6, "OW", ifelse(AT1c$bfscantno==7, "NW", ifelse(AT1c$bfscantno==8, "GL", 
ifelse(AT1c$bfscantno==15, "AR", ifelse(AT1c$bfscantno==16, "AI", "AG")))))))
AT1c$pct <- round(AT1c$pct, digits = 2)
AT1c <- AT1c[,-1]
AT1c <- AT1c[,c(2,1,3,4)]
AT1c$remarks <-c("Full sample of municipalities", "Data request sent to 30 % of municipalities", 
"Data received from 30% of municipalties", "Excluded, majority voting right", 
"Full sample of municipalities", "Excluded, majority voting right", 
"Excluded, majority voting right", "Excluded, majority voting right", 
"Full sample of municipalities", "Full sample of municipalities", 
"Data received from 50% of municipalities", "Full sample", "Data request sent to 30% of municipalities",
"Data received from 30% of municipalities", "Excluded, majority voting right", 
"Excluded, majority voting right", "Data received from 30% of municipalities",
"Full sample of municipalities", "Excluded, data unavailable", 
"Data received from 30% of municipalities", "Full sample of municipalities", 
"Full sample of municipalities", "Data request sent to 30% of municipalities of German speaking region, data from French speaking region not available", 
"Full sample of municipalities", "Full sample of municipalities", "Data from only 9 municipalities online available")

cantons <- c("Zurich", "Berne", "Lucerne", "Uri", "Schwyz","Obwalden", "Nidwalden", "Glarus", "Zug", "Fribourg",
"Solothurn", "Basel-City", "Basel-Country", "Schaffhausen", "Apenzell Innerrhoden","Apenzell Ausserrhoden", "St. Gall", 
"Grissons", "Aargau", "Thurgau", "Ticino", "Vaud", "Valais", "Neuchatel", "Geneva", "Jura")
AT1c$canton.x <- cantons

names(AT1c)
print(xtable((as.matrix(AT1c)), caption = "Municipalities Sample overview",
col.names = c("Canton", "N municipalities", "n municipalities in sample", 
"Percentage municipalities in sample", "Remarks"), format = "pandoc"),
include.rownames=FALSE) 

# ------------------------------------------------------------------ Figure 1 Appendix: Conservative voting in direct democratic ballots, municipalities included/excluded in analysis

# ----- Abstimmungsverhalten

names(d_initiatives_wd)

# ----- Faktor bilden

d_initiatives_wd$i_EinbInit <- 100 - d_initiatives_wd$i_EinbInit
#Nochmals anschauen, ob braucht!!!

#Filter Gemeinden mit >9000 bfsresidence code (keine "Gemeinden" und eine Gemeinde "878" mit Missing values)

d_initiatives_wd <- d_initiatives_wd %>% dplyr::filter(!is.na(i_EinbInit))
d_initiatives_wd <- d_initiatives_wd %>% dplyr::filter(!is.na(i_RegZu))

fit <- factanal(x = d_initiatives_wd[, c("i_RegZu", "i_EinbInit", "i_Minarette", 
                                         "i_AusInit", "i_Massenweinw")], factors = 1, scores = c("regression"), rotation = "none")
print(fit, digits=2, cutoff=.3, sort=TRUE)
fs <- data.frame(fit$scores)
d_initiatives_wd$rowname <- rownames(d_initiatives_wd)
fs$rowname   <- rownames(fs)

d_initiatives_wd <- left_join(d_initiatives_wd, fs, by = "rowname")
names(d_initiatives_wd)[names(d_initiatives_wd)=="Factor1"] <- "dd_cons"

# ----- Calculate median

median_dd_cons <- median(d_initiatives_wd$dd_cons)
describe(d_initiatives_wd$dd_cons)
round(min(d_initiatives_wd$dd_cons), 2)
round(max(d_initiatives_wd$dd_cons), 2)

# ----- Aggregate data from our sample

in_sample <- d_nr15_gde %>%
dplyr::select(dd_cons, bfsresidence_code) %>%
dplyr::group_by(bfsresidence_code) %>%
dplyr::summarize(dd_cons=mean(dd_cons))
in_sample$bfsresidence_code <- as.numeric(as.character(in_sample$bfsresidence_code))
in_sample$dd_cons <- as.numeric(as.character(in_sample$dd_cons))

# ----- Figure

p1 <- ggplot(in_sample, aes(y=dd_cons, x=0)) +
  geom_boxplot(alpha=0.6, fill="grey50", color="grey50", width=0.2) +
  geom_jitter(width = 0.1, alpha = 0.1) +
  theme(plot.title = element_text(size=12, hjust = 0.5)) + theme_bw() +
  labs(y="Index of conservative voting in direct democratic ballots") +
  theme(plot.title = element_text(size=12, hjust = 0.5)) +
  geom_hline(yintercept=median_dd_cons, linetype="dashed", color = "black", size=1) +
  annotate(geom="text", x=-0.05, y=0.3, label="Median Swiss municiaplities", color="black") +
  annotate(geom="text", x=0.06, y=3, label="Municipalities in sample", color="grey50") + 
  theme(axis.title.x=element_blank(),
  axis.text.x=element_blank(),
  axis.ticks.x=element_blank()); p1

jpeg(file="Figures/mun_comp.jpeg", 
width = 5, height = 6, units = "in", res = 300)
plot(p1)
dev.off()

# ------------------------------------------------------------------ Table 3 Appendix: Modified and unmodified ballots, by ideological position of party

d_nr15_cand_b_ideol <- d_nr15_cand %>% 
dplyr::group_by(partypos, list_id) %>% 
dplyr::summarize(bfsballots_changed = mean(bfsballots_changed), 
bfsballots_unchanged = mean(bfsballots_unchanged))

d_nr15_cand_b_ideol <- d_nr15_cand_b_ideol %>% 
dplyr::group_by(partypos) %>% 
dplyr::summarize(changed_ballots = sum(bfsballots_changed), 
unchanged_ballots = sum(bfsballots_unchanged),
perc_changed_ballots = (sum(bfsballots_changed)/(sum(bfsballots_unchanged) + sum(bfsballots_changed)))*100)

d_nr15_cand_b_ideol$partypos <- factor(d_nr15_cand_b_ideol$partypos, 
levels = c(1, 2, 3, 4),
labels = c("Right", "Center", "Left", "Others"))

d_nr15_cand_b_ideol$perc_changed_ballots <- round(d_nr15_cand_b_ideol$perc_changed_ballots, digits = 1)

colnames(d_nr15_cand_b_ideol) <- c("Party ideology","Modified ballots (n)", "Unmodified ballots (n)", "Percentage modified ballots (%)")
print(xtable(as.matrix(d_nr15_cand_b_ideol), caption = "Modified and unmodified ballots, by ideological position of party"), 
include.rownames=FALSE, caption.placement = "top")

# ------------------------------------------------------------------ Table 4 Appendix: Modified and unmodified ballots, by party

names(d_nr15_cand)
table(d_nr15_cand$bfspartyno)
table(d_nr15_cand$bfslist_txt)
table(d_nr15_cand$bfslist_txt, d_nr15_cand$bfspartyno)

d_nr15_cand_b_party <- d_nr15_cand %>% 
dplyr::group_by(bfspartyno, list_id) %>% 
dplyr::summarize(bfsballots_changed = mean(bfsballots_changed), 
bfsballots_unchanged = mean(bfsballots_unchanged))

d_nr15_cand_b_party <- d_nr15_cand_b_party %>% 
dplyr::group_by(bfspartyno) %>% 
dplyr::summarize(changed_ballots = sum(bfsballots_changed), 
unchanged_ballots = sum(bfsballots_unchanged),
perc_changed_ballots = (sum(bfsballots_changed)/(sum(bfsballots_unchanged) + sum(bfsballots_changed)))*100)

d_nr15_cand_b_party$bfspartyno<- factor(d_nr15_cand_b_party$bfspartyno, 
levels = c(1, 2, 3, 4, 7, 8, 9, 13, 15, 16, 18, 27, 31, 32, 33, 35),
labels = c("FDP/PLR (PRD)", "CVP/PDC", "SP/PS", "SVP/UDC", "EVP/PEV", "CSP/PCS", "PdA/PST", 
"GPS/PES", "SD/DS", "EDU/UDF", "Lega", "Sol.", "GLP/PVL", "BDP/PBD", "MCR", "Others"))

d_nr15_cand_b_party$perc_changed_ballots <- round(d_nr15_cand_b_party$perc_changed_ballots, digits = 1)

colnames(d_nr15_cand_b_party) <- c("Party","Modified ballots (n)", "Unmodified ballots (n)", "Percentage modified ballots (%)")
print(xtable(as.matrix(d_nr15_cand_b_party), caption = "Modified and unmodified ballots, by party"), 
include.rownames=FALSE, caption.placement = "top")

# ------------------------------------------------------------------ Table 5 Appendix: Descriptive statistics of the dependent variables

a <- c("npv_rel", "pos_mean", "cum_rel", "pan_mean")
       
AT2a <- d_nr15_cand %>% dplyr::ungroup() %>% 
na.omit() %>%
dplyr::select(a) %>% 
dplyr::summarize_all(c("min", "max", "mean", "sd"))
AT2a <- data.frame(matrix(as.vector(AT2a), nrow = 4, ncol = 4))
AT2a <- sapply(AT2a, as.numeric)
colnames(AT2a) <- c("Min","Max", "Mean", "SD")
rownames(AT2a) <- c("Negative preference votes", "Positive preference votes", "Cumulation", "Panachage")
AT2a <- apply(AT2a, c(1,2), function(x) round(x, 2))
xtable(AT2a)
       
# ------------------------------------------------------------------ Table 6 Appendix: Descriptive statistics of the variables at the candidate and the list level

b <- c("rel_pos", "age_c3", "num_listpos", "nonswissname", 
"bfsincumbency", "precumulated", "bfssex", "partypos")

AT2b <- d_nr15_cand %>% dplyr::ungroup() %>% 
na.omit() %>%
dplyr::select(b) %>% 
dplyr::summarize_all(c("min", "max", "mean", "sd"))
AT2b <- data.frame(matrix(as.vector(AT2b), nrow = 8, ncol = 4))
AT2b <- sapply(AT2b, as.numeric)
colnames(AT2b) <- c("Min","Max", "Mean", "SD")
rownames(AT2b) <- c("Relative position on ballot", 
"Age", "Number of list positions", "Non-Swiss name", "Incumbent", "Pre-cumulated", "Male", 
"Party position")
AT2b <- apply(AT2b, c(1,2), function(x) round(x, 2))
xtable(AT2b)

# ------------------------------------------------------------------ Table 7 Appendix: Descriptive statistics of the municipal-level variables

names(d_nr15_gde)
d_nr15_gde <- d_nr15_gde[!d_nr15_gde$bfscantno.y == 20, ]
b <- c("prop_ogn_foreign_tot", "dd_cons")

AT2 <- d_nr15_gde %>% dplyr::ungroup() %>% 
dplyr::select(b) %>% 
dplyr::summarize_all(c("min", "max", "mean", "sd"))
AT2 <- data.frame(matrix(as.vector(AT2), nrow = 2, ncol = 4))
AT2 <- sapply(AT2, as.numeric)
colnames(AT2) <- c("Min","Max", "Mean", "SD")
rownames(AT2) <- c("Proportion naturalized citizens", 
"Conservative attitudes")
AT2 <- apply(AT2, c(1,2), function(x) round(x, 2))
xtable(AT2)

